package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.ShiroUserRole;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:17
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class ShiroUserRoleDao extends BaseDao<ShiroUserRole> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<ShiroUserRole> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<ShiroUserRole> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<ShiroUserRole> list = run.query(sql, new ResultSetHandler<List<ShiroUserRole>>() {
            @Override
            public List<ShiroUserRole> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    ShiroUserRole shiroUserRole = new ShiroUserRole();

                    shiroUserRole.setId(rs.getInt("id"));
                    shiroUserRole.setRoleId(rs.getInt("role_id"));
                    shiroUserRole.setUserId(rs.getInt("user_id"));

                    list.add(shiroUserRole);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  ShiroUserRole 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public ShiroUserRole selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, role_id, user_id from shiro_user_role where id = ?";
        ShiroUserRole shiroUserRole = run.query(sql,new ResultSetHandler<ShiroUserRole>() {
            @Override
            public ShiroUserRole handle(ResultSet rs) throws SQLException {
                ShiroUserRole shiroUserRole = null;
                if(rs.next()) {
                    shiroUserRole = new ShiroUserRole();

                        shiroUserRole.setId(rs.getInt("id"));
                        shiroUserRole.setRoleId(rs.getInt("role_id"));
                        shiroUserRole.setUserId(rs.getInt("user_id"));
                }
                return  shiroUserRole;
            }
        },params);
        return shiroUserRole;
    }

    /**
    *
    * @param shiroUserRole 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(ShiroUserRole shiroUserRole) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO shiro_user_role (  ");
        sql.append("  role_id, user_id ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            shiroUserRole.getRoleId(),  
            shiroUserRole.getUserId()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  shiroUserRole
    * @return
    * @throws SQLException
    */
    @Override
    public int update(ShiroUserRole shiroUserRole) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   shiro_user_role           ");
        sql.append(" SET                    ");
            
            sql.append("   role_id  = ?,  ");
            sql.append("   user_id  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                shiroUserRole.getRoleId(), 
                shiroUserRole.getUserId(), 
            shiroUserRole.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from shiro_user_role where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
